package dao;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Properties;

import bean.UserBean;

public class DaoUser extends Dao {
	Properties sql = new Properties();
	public DaoUser() {			
		try {
			FileInputStream in = new FileInputStream(config.getProperty("sqlUser"));
			sql.load(in);
			in.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public UserBean connection (UserBean user){
		UserBean u = null;
		try {
			PreparedStatement pst = con.prepareStatement(sql.getProperty("connection"));
			pst.setString(1, user.getLogin());
			pst.setString(2, user.getPassword());
			ResultSet rs = pst.executeQuery();
			while (rs.next()) {
				u = new UserBean ();
				u.setId(rs.getInt("id"));
				u.setLogin(rs.getString("login"));
				u.setPassword(rs.getString("password"));
				u.setRole(rs.getString("role"));
				u.setEmail(rs.getString("email"));
				u.setActived(rs.getBoolean("isActived"));
				}
			pst.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return u;
	}
	
	public UserBean getUserById(int id){
		UserBean u=null;
		try {
			Statement stmt = con.createStatement();
			String sql = "Select * from users where id ='"+id+"';";
			ResultSet rs = stmt.executeQuery(sql);
			while (rs.next()) {
				u = new UserBean ();
				u.setId(rs.getInt("id"));
				u.setLogin(rs.getString("login"));
				u.setPassword(rs.getString("password"));
				u.setRole(rs.getString("role"));
				u.setEmail(rs.getString("email"));
				u.setActived(rs.getBoolean("isActived"));
				}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return u;
	}
	public UserBean CreateUser(UserBean u){
		try {
			Statement stmt = con.createStatement();
			String sql = "INSERT INTO users (login,password,role,email,isActived) " + 
						 " VALUES ('"+u.getLogin()+"','"+u.getPassword()+"','"+u.getRole()+"','"+u.getEmail()+"',0)";
			System.out.println(sql);
			stmt.executeUpdate(sql);
			stmt.close();
			//con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return connection(u);
	}
	
	public UserBean updateUser(UserBean u){
		Statement stmt;
		try {
		stmt = con.createStatement();
		PreparedStatement pst = con.prepareStatement(sql.getProperty("updateUser"));
		pst.setString(1,u.getLogin());
		pst.setString(2,u.getPassword());
		pst.setString(3,u.getRole());
		pst.setString(4,u.getEmail());
		pst.setInt(5,(u.getActived())?1:0);
		pst.setInt(6,u.getId());
		//pst.setInt(5,1);
		pst.executeUpdate();
		pst.close();
		//con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return u;
	}
	public ArrayList<UserBean> getAllUsers(){
		ArrayList<UserBean> userList = new ArrayList<UserBean>();
		try {
			PreparedStatement pstPreparedStatement = con.prepareStatement(sql.getProperty("getAllUsers"));
			userList = rsToArrayList(pstPreparedStatement.executeQuery());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return userList;
		
	}
	private ArrayList<UserBean> rsToArrayList (ResultSet rs){
		ArrayList<UserBean> UserList = new ArrayList<UserBean>(); 
		try {
			while (rs.next()) {
				UserBean u = new UserBean();
				u = new UserBean ();
				u.setId(rs.getInt("id"));
				u.setLogin(rs.getString("login"));
				u.setPassword(rs.getString("password"));
				u.setRole(rs.getString("role"));
				u.setEmail(rs.getString("email"));
				u.setActived(rs.getBoolean("isActived"));
				UserList.add(u);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return UserList;
	}
	
	public void deleteUser (UserBean u){
		PreparedStatement pst;
		try {
			pst = con.prepareStatement(sql.getProperty("deleteUser"));
			pst.setInt(1,u.getId());
			pst.executeUpdate();
			pst.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}

